package com.vue.admin.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.db.SqlBuilder;
import com.example.db.TableService;
import com.example.db.model.IPage;
import com.example.utils.StringUtils;
import com.vue.admin.system.model.SysMenu;
import com.vue.admin.system.service.MenuService;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Classname MenuServiceImpl
 * @Description TODO
 * @Date 2022/8/20 19:16
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    TableService tableService;

    @Override
    public SysMenu save(SysMenu sysMenu) {
        tableService.save(sysMenu,SysMenu.class);
        return sysMenu;
    }

    @Override
    public SysMenu getSysMenuById(Object id) {
        SysMenu sysMenu=tableService.getOneById(id,SysMenu.class);
        return sysMenu;
    }

    @Override
    public SysMenu updateById(SysMenu sysMenu) {
        tableService.updateById(sysMenu,SysMenu.class);
        return sysMenu;
    }

    @Override
    public IPage<SysMenu> sysMenuPage(Integer pageNum, Integer pageSize, Object obj) {
        return null;
    }

    @Override
    public List<SysMenu> list() {
        List<SysMenu> sysMenuList=tableService.getList(SysMenu.class);
        return sysMenuList;
    }

    @Override
    public SysMenu getSysMenuByComponent(String compontent) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(SysMenu.class);
        sqlBuilder.and("component").eq(compontent);
        return tableService.selectOne(sqlBuilder,SysMenu.class);
    }

    @Override
    public SysMenu getSysMenuByPath(String path) {
        SqlBuilder sqlBuilder = tableService.getSqlBuilder(SysMenu.class);
        sqlBuilder.and("path").eq(path);
        return tableService.selectOne(sqlBuilder, SysMenu.class);
    }

    @Override
    public JSONArray getUserMenuList() {
        List<SysMenu> sysMenuList=list();
        JSONArray array=dealMenuList(sysMenuList);
        return array;
    }

    private JSONArray dealMenuList(List<SysMenu> sysMenuList){
        JSONArray array=new JSONArray();
        sysMenuList.stream().filter(e->{
            return String.valueOf(e.getParentId()).equals("0") &&! e.getMenuType().equals("F");
        }).map((e)->{

            JSONObject obj = getMenuJson(e);
            obj.put("alwaysShow", true);
            obj.put("children", getChildList(String.valueOf(e.getMenu_id()), sysMenuList));
            array.add(obj);

            return e;
        }).collect(Collectors.toList());
        return array;
    }

    private  JSONArray getChildList(String id,List<SysMenu> all){
        JSONArray array=new JSONArray();
        all.stream().filter(
                e->{
                    return Objects.equals(String.valueOf(e.getParentId()),id)
                            &&
                            ! e.getMenuType().equals("F");
                }
        ).map(
                (m)->{

                    JSONObject obj=getMenuJson(m);
                    if(StringUtils.isEmpty(m.getComponent())){
                        obj.put("component","ParentView");
                    }
                    obj.put("children",getChildList(String.valueOf(m.getMenu_id()),all));
                    array.add(obj);

                    return m;
                }
        ).collect(Collectors.toList());
        return array;
    }


    private JSONObject getMenuJson(SysMenu sysMenu){
        JSONObject obj=new JSONObject();
        obj.put("name",sysMenu.getMenuName());
        if(sysMenu.getParentId()==0){
            obj.put("path","/"+sysMenu.getPath());
        }else {
            obj.put("path",sysMenu.getPath());
        }
        obj.put("hidden",false);
        if(sysMenu.getComponent()==null){
            sysMenu.setComponent("Layout");
        }
        obj.put("component",sysMenu.getComponent());

        JSONObject meta=new JSONObject();
        meta.put("icon",sysMenu.getIcon());
        meta.put("title",sysMenu.getMenuName());
        meta.put("noCache",sysMenu.getIsCache()==0);
        meta.put("link",null);
        obj.put("meta",meta);
        return obj;
    }
}
